package com.atguigu.tingshu.payment.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private KafkaService kafkaService;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo) {
        //保存交易信息
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, orderInfo.getOrderNo());
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        if (paymentInfo == null) {
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPaymentType(orderInfo.getItemType());
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            paymentInfo.setPayWay(orderInfo.getPayWay());
            paymentInfo.setOutTradeNo(orderInfo.getOrderNo());
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            paymentInfo.setContent(orderInfo.getOrderTitle());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            this.save(paymentInfo);
        }
    }
    @Override
    public void savePaymentInfo(RechargeInfo rechargeInfo) {
        //保存支付信息
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderNo, rechargeInfo.getOrderNo());
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        if (paymentInfo == null) {
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(rechargeInfo.getUserId());
            paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_RECHARGE);
            paymentInfo.setOrderNo(rechargeInfo.getOrderNo());
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            paymentInfo.setOutTradeNo(rechargeInfo.getOrderNo());
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            this.save(paymentInfo);
        }
    }
    @Override
    public void updatePaymentStatus(Transaction transaction) {
        //查询支付订单信息
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, transaction.getOutTradeNo());
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        if(SystemConstant.PAYMENT_STATUS_PAID.equals(paymentInfo.getPaymentStatus())){
            return;
        }
        //更新支付订单信息
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        paymentInfo.setOrderNo(transaction.getOutTradeNo());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
        this.updateById(paymentInfo);
        String paymentType = paymentInfo.getPaymentType();
        if(SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)){
            // 向MQ中发送更新订单状态的消息
            log.info("交易信息更新完毕,发送更改订单状态消息：{}", paymentInfo.getOrderNo());
            kafkaService.sendMsg(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS, paymentInfo.getOrderNo());
            log.info("交易信息更新完毕,发送更改订单状态消息成功：{}", paymentInfo.getOrderNo());
        }else if(SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)){
            log.info("交易信息更新完毕,发送更改充值订单消息：{}", paymentInfo.getOrderNo());
            kafkaService.sendMsg(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS, paymentInfo.getOrderNo());
            log.info("交易信息更新完毕,发送更改种植订单消息成功：{}", paymentInfo.getOrderNo());
        }



    }
}
